<div class="layui-fluid" id="VIEW-index" lay-title="首页" lig-title="idx-title">
  <div class="layui-card">
    <div class="layui-card-header layui-bg-blue" style="font-size:16px;" lig-lang="label-searchTitle">筛选数据</div>
    <div class="layui-card-body">
      <form class="layui-form" lay-filter="lig-index-searchform">
        <div class="layui-row">
          <div class="layui-inline">
            <label class="layui-form-label" lig-lang="pd-customer">客户</label>
            <div class="layui-inline">
              <select name="customerCode">
                <option value=""></option>
              </select>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label" style="width:85px;" lig-lang="idx-xssjqz">销售起止时间</label>
            <div class="layui-inline">
              <input type="text" name="xssjQs" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-inline">--</div>
            <div class="layui-inline">
              <input type="text" name="xssjJs" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-row layui-col-space10">
              <div class="layui-col-xs6">
                <div class="layui-btn layui-btn-sm layui-btn-fluid" lay-filter="lig-index-search" lig-lang="btn-search">查询</div>
              </div>
              <div class="layui-col-xs6">
                <button type="reset" class="layui-btn layui-btn-sm layui-btn-primary" lig-lang="btn-reset">重置</button>
              </div>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>
  <div class="layui-card">
    <div class="layui-card-header layui-bg-blue" style="font-size:16px;" lig-lang="pd-pdj">盘点机</div>
    <div class="layui-card-body">
      <table class="layui-table" lay-skin="line">
        <thead>
          <tr>
            <th style="width:50%;" lig-lang="pd-customer">客户</th>
            <th style="width:50%;" lig-lang="idx-sl">数量</th>
          </tr>
        </thead>
        <tbody id="machine-data">

        </tbody>
      </table>
    </div>
  </div>
  <div class="layui-card">
    <div class="layui-card-header layui-bg-blue" style="font-size:16px;" lig-lang="pd-tag">标签</div>
    <div class="layui-card-body">
      <table class="layui-table" lay-skin="line">
        <thead>
        <tr>
          <th style="width:50%;" lig-lang="pd-customer">客户</th>
          <th style="width:50%;" lig-lang="idx-sl">数量</th>
        </tr>
        </thead>
        <tbody id="tag-data">

        </tbody>
      </table>
    </div>
  </div>
</div>
<script type="text/html" id="tpl-machine-list">
  {{# layui.each(d.machine, function(i, it){ }}
  <tr>
    <td>{{ it.customerName }}</td>
    <td>{{ it.sl }}</td>
  </tr>
  {{#  }); }}

  {{#  if(!d.machine || d.machine.length == 0){ }}
  <tr><td colspan="2" lig-lang="label-noData">无数据</td></tr>
  {{#  } }}
</script>
<script type="text/html" id="tpl-tag-list">
  {{# layui.each(d.tag, function(i, it){ }}
  <tr>
    <td>{{ it.customerName }}</td>
    <td>{{ it.sl }}</td>
  </tr>
  {{#  }); }}

  {{#  if(typeof d.tag == 'undefined' || d.tag.length == 0){ }}
  <tr><td colspan="2" lig-lang="label-noData">无数据</td></tr>
  {{#  } }}
</script>
<script>
layui.use( ['jquery', 'form', 'table', 'laydate', 'laytpl', 'lig'], function($, form, table, laydate, laytpl, lig) {
  var _searchForm = '[lay-filter="lig-index-searchform"]';
  lig.selectCustomerList(_searchForm + '  select[name="customerCode"]');
  var init_xssjQs = getDate();

  laydate.render({
    elem: _searchForm + ' input[name="xssjQs"]',
    value: init_xssjQs
  });
  laydate.render({
    elem: _searchForm + ' input[name="xssjJs"]'
  });
  form.render();
  load({xssjQs: init_xssjQs});

  $('[lay-filter="lig-index-search"]').click(function () {
    var dataJSON = lig.form2JSON('[lay-filter="lig-index-searchform"]');
    load(dataJSON);
  });

  function load(data) {
    lig.ajaxget({
      url: 'pd/index/search',
      data: data,
      success: function (result) {
        laytpl($('#tpl-machine-list').html()).render(result.data, function(html){
          $('#machine-data').html(html);
          lig.filter($('#machine-data'));
        });

        laytpl($('#tpl-tag-list').html()).render(result.data, function(html){
          $('#tag-data').html(html);
          lig.filter($('#tag-data'));
        });
      }
    });
  }


  // 补零函数
  function doHandleZero(zero) {
    var date = zero;
    if (zero.toString().length == 1) {
      date = "0" + zero;
    }
    return date;
  }

  // 获取每个月一号
  function getDate() {
    var myDate = new Date();
    var tYear = myDate.getFullYear()
    var tMonth = myDate.getMonth()
    tMonth = doHandleZero(tMonth + 1)

    return tYear + "-" + tMonth + "-01";
  }
});
</script>
